619. Biggest Single Number


Posted by ikl258794613 on 2024-02-26

Table: MyNumbers

Column Name Type
num int

This table may contain duplicates (In other words, there is no primary key for this table in SQL).
Each row of this table contains an integer.

A single number is a number that appeared only once in the MyNumbers table.

Find the largest single number. If there is no single number, report null.

The result format is in the following example.

Example 1:

Input:
MyNumbers table:

num
8
8
3
3
1
4
5
6

Output:

num
6

Explanation: The single numbers are 1, 4, 5, and 6.
Since 6 is the largest single number, we return it.
Example 2:

Input:
MyNumbers table:

num
8
8
7
7
3
3
3

Output:

num
null

Explanation: There are no single numbers in the input table so we return null.


SELECT Max(num) AS num
FROM (
    SELECT num 
    FROM MyNumbers
    HAVING COUNT(num) = 1
     )
     AS SingleNumber

學習點:
1.使用FROM子查詢的表,一定要AS成新的表格不然會語法錯誤
2.HAVING用在聚合函數上


#SQL







Related Posts

《鳥哥 Linux 私房菜:基礎篇》Chapter 05 - Linux 的檔案權限與目錄配置

《鳥哥 Linux 私房菜:基礎篇》Chapter 05 - Linux 的檔案權限與目錄配置

F2E合作社|共用項目margin與padding|Bootstrap 5網頁框架開發入門

F2E合作社|共用項目margin與padding|Bootstrap 5網頁框架開發入門

「新手問題」為什麼我不能下載 npm 的套件?

「新手問題」為什麼我不能下載 npm 的套件?


Comments